package com.weiyi.chat.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.weiyi.chat.R;
import com.weiyi.chat.api.ISystemMsg;
import com.weiyi.chat.common.Constant;
import com.weiyi.chat.common.base.BaseActivity;
import com.weiyi.chat.common.base.MyBaseAdapter;
import com.weiyi.chat.domain.SystemMsgList;
import com.weiyi.chat.presenter.SystemMsgPresenter;
import com.weiyi.chat.utils.PreferenceUtil;
import com.weiyi.chat.utils.ToastUtil;
import com.weiyi.chat.view.widget.HeadLayout;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SystemMsgActivity extends BaseActivity implements ISystemMsg.Views, BaseRefreshListener {

    @BindView(R.id.h_layout)
    HeadLayout mHLayout;
    @BindView(R.id.list)
    ListView mList;
    @BindView(R.id.pull_to_refresh)
    PullToRefreshLayout mPullToRefresh;
    private SystemMsgPresenter mMsgPresenter;
    private MyBaseAdapter<SystemMsgList.BodyBean.DataBean> mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_system_msg;
    }

    @Override
    protected void initView() {
        mHLayout.setTitle("公告列表");
        mMsgPresenter = new SystemMsgPresenter(this);
        mPullToRefresh.setRefreshListener(this);
        mMsgPresenter.requestSystemMsg();

        mAdapter = new MyBaseAdapter<>(new MyBaseAdapter.initView<SystemMsgList.BodyBean.DataBean>() {
            @Override
            public View initItenView(int position, View convertView, ViewGroup parent, SystemMsgList.BodyBean.DataBean obj) {
                ViewHolder holder;
                if (convertView == null){
                    convertView = LayoutInflater.from(mAct).inflate(R.layout.item_system_msg, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                }

                holder = (ViewHolder) convertView.getTag();
                holder.mMsgTitle.setText(obj.getNtc_title());
                holder.mMsgContent.setText(getDateTimeFromMillisecond(obj.getNtc_create_at() * 1000));

                return convertView;
            }
        });
        mList.setAdapter(mAdapter);
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(new Intent(mAct, MsgContentActivity.class).putExtra("content", mAdapter.getItem(i).getNtc_body()));
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResetSucess(SystemMsgList.BodyBean body) {
        mPullToRefresh.finishRefresh();
        mAdapter.setList(body.getData());
    }

    @Override
    public void showError(String msg) {
        ToastUtil.showTextToast(mAct, msg);
        if (Constant.TOKEN_FIAL.equals(msg)){
            ToastUtil.showTextToast(mAct, "请重新登录");
            PreferenceUtil.clearAll(mAct);
            toLogin();
        }
    }

    @Override
    public void refresh() {
        mMsgPresenter.requestSystemMsg();
    }

    @Override
    public void loadMore() {
        mPullToRefresh.finishLoadMore();
    }


    //毫秒转成时间格式


    public static String getDateTimeFromMillisecond(Long time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        String format = simpleDateFormat.format(date);
        return format;
    }


    public class ViewHolder {

        @BindView(R.id.msg_title)
        TextView mMsgTitle;
        @BindView(R.id.msg_content)
        TextView mMsgContent;
        public ViewHolder(View convertView) {
            ButterKnife.bind(this, convertView);
        }
    }
}
